<template>
  <div class="character-info">
    <template v-if="character">
      <div class="info-card">
        <div class="info-header">
          <h3><i class="mdi mdi-account-details"></i> 角色信息</h3>
          <button class="edit-btn" @click="toggleEditMode">
            <i class="mdi" :class="editMode ? 'mdi-close' : 'mdi-pencil'"></i>
            {{ editMode ? '取消' : '编辑' }}
          </button>
        </div>

        <div v-if="!editMode" class="info-content">
          <div class="info-row">
            <span class="info-label">角色名称</span>
            <span class="info-value">{{ character.name  }}</span>
          </div>
          <div class="info-row">
            <span class="info-label">角色前缀</span>
            <span class="info-value" v-html="MinecraftFormatter.parse(character.prefix)"></span>
          </div>
          <div class="info-row">
            <span class="info-label">所在组</span>
            <span class="info-value" v-html="MinecraftFormatter.parse(character.group)"></span>
          </div>
          <div class="info-row">
            <span class="info-label">创建时间</span>
            <span class="info-value">{{ formatDate(character.createdAt)  }}</span>
          </div>
          <div class="info-row">
            <span class="info-label">最后登录</span>
            <span class="info-value">{{ formatDate(new Date()) }}</span>
          </div>
        </div>

        <div v-else class="edit-content">
          <div class="form-group">
            <label>角色名称</label>
            <input
                v-model="character.name"
                type="text"
                placeholder="输入角色名"
            >
          </div>
          <div class="form-group">
            <label>角色前缀</label>
            <input
                v-model="character.prefix"
                type="text"
                placeholder="输入角色前缀"
            >
          </div>
          <div class="form-group">
            <label>所在组</label>
            <input
                v-model="character.group"
                type="text"
                placeholder="输入所在组"
            >
          </div>

          <div class="info-actions">
            <button class="save-btn" @click="saveChanges">
              <i class="mdi mdi-content-save"></i> 保存更改
            </button>

            <div class="more-actions">
              <button class="toggle-more" @click="toggleMoreActions">
                <i class="mdi" :class="showMoreActions ? 'mdi-chevron-up' : 'mdi-chevron-down'"></i>
                更多操作
              </button>
              <div v-if="showMoreActions" class="more-actions-content">
                <button class="delete-btn" @click="deleteCharacter">
                  <i class="mdi mdi-delete"></i> 删除角色
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
    <div v-else class="info-placeholder">
      <i class="mdi mdi-arrow-left"></i>
      <p>请从左侧选择角色</p>
    </div>

    <!-- 确认删除弹窗 -->
    <el-dialog
        v-model="showDeleteConfirm"
        title="确认删除"
        width="400px"
    >
      <span>确定要删除 {{ deleteCharacterName }} 吗？</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showDeleteConfirm = false">取消</el-button>
          <el-button type="danger" @click="confirmDelete">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { MinecraftFormatter } from '@/utils/color-util.js'

const props = defineProps({
  character: {
    type: Object,
    default: null
  }
})

const emit = defineEmits(['update-character', 'delete-character'])

const editMode = ref(false)
const showMoreActions = ref(false)
const showDeleteConfirm = ref(false)
const deleteCharacterName = ref('')

const formatDate = (date) => {
  return date.toLocaleDateString()  + ' ' + date.toLocaleTimeString()
}

const toggleEditMode = () => {
  editMode.value  = !editMode.value
  showMoreActions.value  = false
}

const toggleMoreActions = () => {
  showMoreActions.value  = !showMoreActions.value
}

const saveChanges = () => {
  editMode.value  = false
  emit('update-character', props.character)
  ElMessage.success(' 角色信息已保存')
}

const deleteCharacter = () => {
  deleteCharacterName.value  = props.character.name
  showDeleteConfirm.value  = true
}

const confirmDelete = () => {
  emit('delete-character', props.character.id)
  showDeleteConfirm.value  = false
}
</script>

<style scoped>
.character-info {
  width: 800px;
  padding: 0;
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, rgba(19, 20, 31, 0.8) 0%, rgba(15, 17, 26, 0.9) 100%);
}

.info-card {
  padding: 24px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.info-header h3 {
  margin: 0;
  color: #4e7de4;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 600;
  text-shadow: 0 0 10px rgba(78, 125, 228, 0.3);
}

.edit-btn {
  padding: 8px 16px;
  background: linear-gradient(135deg, rgba(78, 125, 228, 0.3) 0%, rgba(78, 125, 228, 0.2) 100%);
  color: #e0e3ff;
  border: 1px solid rgba(78, 125, 228, 0.3);
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
}

.edit-btn:hover {
  background: linear-gradient(135deg, rgba(78, 125, 228, 0.4) 0%, rgba(78, 125, 228, 0.3) 100%);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(78, 125, 228, 0.2);
}

.info-content {
  flex: 1;
}

.info-row {
  display: flex;
  margin-bottom: 18px;
  padding: 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.05);
  transition: all 0.2s;
}

.info-row:hover {
  background: rgba(255, 255, 255, 0.07);
  border-color: rgba(78, 125, 228, 0.2);
}

.info-label {
  width: 100px;
  color: #8b93b0;
  font-size: 14px;
  font-weight: 500;
}

.info-value {
  flex: 1;
  color: #e0e3ff;
  font-size: 14px;
  font-weight: 500;
}

.edit-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #8b93b0;
  font-size: 14px;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  background-color: rgba(15, 17, 26, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  color: #e0e3ff;
  font-size: 14px;
  transition: all 0.2s;
}

.form-group input:focus {
  border-color: #4e7de4;
  box-shadow: 0 0 0 2px rgba(78, 125, 228, 0.2);
  outline: none;
}

.info-actions {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.save-btn {
  padding: 14px;
  background: linear-gradient(135deg, #4e7de4 0%, #10b981 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
  box-shadow: 0 2px 10px rgba(16, 185, 129, 0.3);
}

.save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(16, 185, 129, 0.4);
}

.more-actions {
  display: flex;
  flex-direction: column;
  margin-top: 8px;
}

.toggle-more {
  padding: 10px;
  background: transparent;
  color: #8b93b0;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.toggle-more:hover {
  background: rgba(255, 255, 255, 0.05);
}

.more-actions-content {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.delete-btn {
  padding: 10px;
  background: rgba(239, 68, 68, 0.2);
  color: #ef4444;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.delete-btn:hover {
  background: rgba(239, 68, 68, 0.3);
}

.info-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #8b93b0;
}

.info-placeholder i {
  font-size: 48px;
  margin-bottom: 16px;
  color: rgba(255, 255, 255, 0.2);
}

.info-placeholder p {
  font-size: 16px;
  margin: 0;
}

@media (max-width: 1200px) {
  .character-info {
    width: 50%;
    order: 2;
    border-right: none;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  }
}

@media (max-width: 768px) {
  .character-info {
    width: 100%;
  }
}
</style>